package com.zhl.test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.protocol.Protocol;

import com.zhl.test.HTTPSSecureProtocolSocketFactory;

public class 方法一 {

	public static void main(String[] args){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(new Date());
		//从12306查询当天广州东——深圳的火车票
		String a = https("https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date="+date+"&leftTicketDTO.from_station=GGQ&leftTicketDTO.to_station=SZQ&purpose_codes=ADULT");
		
		/*
		 * 这样取出字符串，就可以想怎么操作就怎么操作了
		 */
		
		System.out.println(a);
	
	}
		
	public static String https(String url) {
		String result = "";
		Protocol https = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443);
		Protocol.registerProtocol("https", https);
		GetMethod get = new GetMethod(url);
		HttpClient client = new HttpClient();
		try {
			//client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
			/* 从连接池中取连接的超时时间*/ 
			client.setTimeout(1000); 
			/*连接超时*/ 
			client.getHttpConnectionManager().getParams().setConnectionTimeout(2000); 
			/*请求超时*/
			client.getHttpConnectionManager().getParams().setSoTimeout(4000);
			client.executeMethod(get);
			result = get.getResponseBodyAsString();
			//result = new String(result.getBytes("UTF-8"), "ISO-8859-1");
		Protocol.unregisterProtocol("https");
			return result;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return "error";
	}
}
